import {defineStore} from "pinia";
import IScroll from 'iscroll';

// 定义甘特块和甘特行的接口
interface GanttBlock {
    id: string;
    name: string;
}

interface GanttRow {
    id: string;
    name: string;
    start: string,
    end: string,
    blocks: GanttBlock[];
}

export const useGanttSettingStore = defineStore({
    id: 'ganttSettingStore',
    state: (): {
        filterBlockId: string;
        currentBlock: GanttBlock | {};
        currentRow: GanttRow | {};
        cutBlock: GanttBlock | {};
        cutRow: GanttRow | {};
        targetBlock: GanttBlock | {};
        targetRow: GanttRow | {};
        handleBlock: GanttBlock | {};
        handleRow: GanttRow | {};
        showRowList: GanttRow[];
        rawRowList: GanttRow[];
        showMovedBlock: boolean;
        showDragConfirm: boolean;
        scrollPosition: { any };
        scroller: IScroll | null;
        dataRow: Number;
    } => ({
        filterBlockId: "", // 筛选的甘特块id
        currentBlock: {}, //当前点击的甘特块
        currentRow: {}, //当前点击的甘特行
        cutBlock: {}, //剪切的甘特块
        cutRow: {}, //剪切的甘特行
        targetBlock: {}, //目标甘特块
        targetRow: {}, //目标甘特行
        handleBlock: {}, //右键操作的甘特块
        handleRow: {}, //右键操作的甘特行
        showRowList: [], // 筛选后在界面显示的列数据
        rawRowList: [], // 列原始数据，用于恢复
        showMovedBlock: true, // 是否显示拖拽之前的甘特状态
        showDragConfirm: false, // 调整任务时是否显示确认弹窗
        scrollPosition: {x: 0, y: 0}, // 存储当前的滚动位置
        scroller: null as IScroll | null, // 存储IScroll实例
        dataRow: 0, // 数据第几行
    }),
    getters: {
        // 计算属性：过滤后的行列表
        filteredRowList(): GanttRow[] {
            if (!this.filterBlockId) {
                return this.showRowList;
            }
            return this.showRowList.filter(row =>
                row.blocks.some(block => block.id === this.filterBlockId)
            );
        },

        // 其他可能的计算属性...

        // 例如：当前选中的甘特块是否存在
        isCurrentBlockSet(): boolean {
            return Object.keys(this.currentBlock).length > 0 && this.currentBlock.id !== undefined;
        },

        // 例如：是否有被剪切的甘特块
        hasCutBlock(): boolean {
            return Object.keys(this.cutBlock).length > 0 && this.cutBlock.id !== undefined;
        },
        getFilterBlockId(): string {
            return this.filterBlockId
        },
        getCurrentBlock(): GanttBlock | {} {
            return this.currentBlock
        },
        getCurrentRow(): GanttRow | {} {
            return this.currentRow
        },
        getCutBlock(): GanttBlock | {} {
            return this.cutBlock
        },
        getCutRow(): GanttRow | {} {
            return this.cutRow
        },
        getTargetBlock(): GanttBlock | {} {
            return this.targetBlock
        },
        getTargetRow(): GanttRow | {} {
            return this.targetRow
        },
        getHandleBlock(): GanttBlock | {} {
            return this.handleBlock
        },
        getHandleRow(): GanttRow | {} {
            return this.handleRow
        },
        getShowRowList(): GanttRow[] {
            return this.showRowList
        },
        getRawRowList(): GanttRow[] {
            return this.rawRowList
        },
        getDataRow(): Number {
            return this.dataRow
        },
        getShowMovedBlock(): boolean {
            return this.showMovedBlock
        },
        getShowDragConfirm(): boolean {
            return this.showDragConfirm
        },
    },
    actions: {
        setFilterBlockId(str: string) {
            this.filterBlockId = str;
        },
        setCurrentBlock(object: GanttBlock | {}) {
            this.currentBlock = object;
        },
        setCurrentRow(object: GanttRow | {}) {
            this.currentRow = object;
        },
        setCutBlock(object: GanttBlock | {}) {
            this.cutBlock = object;
        },
        setCutRow(object: GanttRow | {}) {
            this.cutRow = object;
        },
        setTargetBlock(object: GanttBlock | {}) {
            this.targetBlock = object;
        },
        setTargetRow(object: GanttRow | {}) {
            this.targetRow = object;
        },
        setHandleBlock(object: GanttBlock | {}) {
            this.handleBlock = object;
        },
        setHandleRow(object: GanttRow | {}) {
            this.handleRow = object;
        },
        setShowRowList(object: GanttRow[]) {
            this.showRowList = object;
        },
        setRawRowList(object: GanttRow[]) {
            this.rawRowList = object;
        },
        setDataRow(n: Number) {
            this.dataRow = n;
        },
        setShowMovedBlock(bool: boolean) {
            this.showMovedBlock = bool;
        },
        setShowDragConfirm(bool: boolean) {
            this.showDragConfirm = bool;
        },
        scrollToPosition(position: { x: number, y: number }, duration: number = 600) {
            if (this.scroller) {
                this.scroller.scrollTo(position.x, position.y, duration);
            }
        },
        refresh() {
            if (this.scroller) {
                this.scroller.refresh();
            }
        },
    }
})

